<template>
  <FormulaTextAreaConfig
    :value.sync="innerValue"
    :title.sync="innerTitle"
    modalTitle="关联数据过滤条件"
    actionType="FormulaActionsForSQL"
    :getFieldById="getFieldById"
    :form-definition="targetFormDefinition"
    :variableContainerConfig="variableContainerConfig"/>
</template>
<script>
import FormulaTextAreaConfig
  from '@comp/hhd/design/FormDesigner/field/comps/form/FieldValueFormulaGroups/configs/FormulaTextAreaConfig.vue';
import {createProps} from '@/utils';
import {createFormDefinition} from '@comp/hhd/design/FormDesigner/types';
import {getFieldConfigById} from '@/utils/FormDesignUtils';

export default {
  name: 'RelationSystemQueryFormula',
  components: {FormulaTextAreaConfig},
  props: {
    sourceFormDefinition: createProps(Object, createFormDefinition),
    targetFormDefinition: createProps(Object, createFormDefinition),
    value: createProps(String),
    title: createProps(String),
    sourceFormDefinitionIds: createProps(String), //逗号隔开的id列表
    targetFormDefinitionIds: createProps(String), //逗号隔开的id列表
  },
  data() {
    return {};
  },
  methods: {
    changeValue(v) {
      this.$emit('update:value', v);
    },
    getFieldById(id) {
      return getFieldConfigById(id, this.sourceFormDefinition) || getFieldConfigById(id, this.targetFormDefinition);
    },
    changeTitle(v) {
      this.$emit('update:title', v);
    },
  },
  computed: {
    variableContainerConfig() {
      return {
        containers: [
          {
            title: '(简称a表)' + this.sourceFormDefinition.formName,
            key: 'aaa',
            name: 'formVariableForSQL',
            config: {
              formDefinition: this.sourceFormDefinition,
              formIds: this.sourceFormDefinitionIds,
            },
            prefix: 'a_',
          },
          {
            title: '(简称b表)' + this.targetFormDefinition.formName,
            key: 'bbb',
            name: 'formVariableForSQL',
            config: {
              formDefinition: this.targetFormDefinition,
              formIds: this.targetFormDefinitionIds
            },
            prefix: 'b_',
          }
        ],
        formDefinition: this.sourceFormDefinition,
      };
    },
    innerTitle: {
      get() {
        return this.title;
      },
      set(v) {
        this.changeTitle(v);
      }
    },
    innerValue: {
      get() {
        return this.value;
      },
      set(v) {
        this.changeValue(v);
      },
    }
  }
};
</script>